package com.study.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.study.enums.HttpStatusEnum;
import com.study.mapper.StudentDataMapper;
import com.study.model.dto.StudentDataQueryDTO;
import com.study.model.entity.StudentDataDO;
import com.study.model.vo.PageVO;
import com.study.result.AjaxResult;
import com.study.service.IStudentDataService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class StudentDataServiceImpl implements IStudentDataService {

    @Resource
    private StudentDataMapper studentDataMapper;


    @Override
    public AjaxResult selectStudentDataById(Long id) {
        StudentDataDO studentDataDO = studentDataMapper.selectStudentDataById(id);
        if (studentDataDO == null) {
            return AjaxResult.fail(HttpStatusEnum.RESOURCE_NOT_FOUND.getCode(), HttpStatusEnum.RESOURCE_NOT_FOUND.getMsg());
        }
        return AjaxResult.success(studentDataDO);
    }

    @Override
    public AjaxResult selectStudentDataList(StudentDataQueryDTO studentDataQueryDTO) {
        Page<StudentDataDO> page;
        if (studentDataQueryDTO.getPageNum()==null || studentDataQueryDTO.getPageSize()==null) {
            page = PageHelper.startPage(1, 10);
        } else {
            page = PageHelper.startPage(studentDataQueryDTO.getPageNum(), studentDataQueryDTO.getPageSize());
        }
        List<StudentDataDO> list = studentDataMapper.selectStudentDataList(studentDataQueryDTO);
        return AjaxResult.success(PageVO.getPageVO(page, list));
    }

    @Override
    public AjaxResult insertStudentData(StudentDataDO studentDataDO) {
        studentDataDO.setCreateTime(new Date());
        studentDataDO.setUpdateTime(new Date());
        return AjaxResult.toAjax(studentDataMapper.insertStudentData(studentDataDO));
    }

    @Override
    public AjaxResult updateStudentData(StudentDataDO studentDataDO) {
        if (studentDataDO.getId()==null) {
            return AjaxResult.fail("ID不能为空");
        }
        studentDataDO.setUpdateTime(new Date());
        return AjaxResult.toAjax(studentDataMapper.updateStudentData(studentDataDO));
    }

    @Override
    public AjaxResult deleteStudentDataById(Long id) {
        return AjaxResult.toAjax(studentDataMapper.deleteStudentDataById(id));
    }

    @Override
    public AjaxResult deleteStudentDataByIds(Long[] ids) {
        return AjaxResult.toAjax(studentDataMapper.deleteStudentDataByIds(ids));
    }

}
